﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin : System.Web.UI.Page
{
    Service sv;
    protected void Page_Load(object sender, EventArgs e)
    {
        RoomManager.Visible = false;
        NewsManager.Visible = false;
        AccManager.Visible = true;
        string id = Request.QueryString.Get("id");
        sv = new Service();
        DatabindTable();
        if (id == "room")
        {
            RoomManager.Visible = true;
            AccManager.Visible = false;
            
        }
        else if (id == "news")
        {
            NewsManager.Visible = true;
            AccManager.Visible = false;
        }
        else if (id == "admincp")
        {
            AccManager.Visible = true;
        }
    }
    private void DatabindTable()
    {
        string sql = "select * from loaiphong";
        this.gr_Phong.DataSource = sv.GetDataTable(sql);
        this.gr_Phong.DataBind();
        sql = "select * from taikhoan";
        this.GridView1.DataSource = sv.GetDataTable(sql);
        this.GridView1.DataBind();
        sql = "select * from tintuc";
        this.GridView2.DataSource = sv.GetDataTable(sql);
        this.GridView2.DataBind();
    }
    string Upload()
    {
        string fileName;
        if (this.FileUpload1.HasFile)
        {
            fileName = "./images/" + this.FileUpload1.FileName;
            this.FileUpload1.PostedFile.SaveAs(Server.MapPath(fileName));
            return fileName;
        }
        return null;
    }
    protected void btn_Themphong_Click(object sender, EventArgs e)
    {
        string sql = "insert into LoaiPhong(maloai,tenloai,gia,mota,hinhanh) values('" + txt_Maloai.Text + "',N'" + txt_Tenloai.Text + "'," + int.Parse(txt_Gia.Text) + ",N'" + txt_MotaPhong.Text + "','" + Upload() + "')";
        sv.ExeCuteSQL(sql);
        DatabindTable();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string sql = "delete from LoaiPhong where maloai=" + txt_Maloai.Text;
        sv.ExeCuteSQL(sql);
        DatabindTable();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string sql = "delete from LoaiPhong where tenloai=" + txt_Tenloai.Text;
        sv.ExeCuteSQL(sql);
        DatabindTable();
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        string sql = "insert into TinTuc(matintuc,tieude,hinhanh,mota,noidung) values('" + txt_Matin.Text + "',N'" + txt_Tieude.Text + "','" + Upload() + "',N'" + txt_Motatin.Text + "',N'" + txt_Noidungtin.Text + "')";
        sv.ExeCuteSQL(sql);
        DatabindTable();
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        string sql = "delete from TinTuc where matintuc=" + txt_Matin.Text;
        sv.ExeCuteSQL(sql);
        DatabindTable();
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        string sql = "delete from TinTuc where tieude=" + txt_Tieude.Text;
        sv.ExeCuteSQL(sql);
        DatabindTable();
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        string sql = "insert into TaiKhoan(matk,tentk,matkhau) values('" + txt_Matk.Text + "',N'" + txt_Tentk.Text + "','" + txt_Matkhau.Text + "')";
        sv.ExeCuteSQL(sql);
        DatabindTable();
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        string sql = "delete from TaiKhoan where matk=" + txt_Matk.Text;
        sv.ExeCuteSQL(sql);
        DatabindTable();
    }
    protected void Button9_Click(object sender, EventArgs e)
    {
        string sql = "delete from TaiKhoan where tentk=" + txt_Tentk.Text;
        sv.ExeCuteSQL(sql);
        DatabindTable();
    }
}